<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.code"
          clearable
          placeholder="訂單編號"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.vendor"
          clearable
          placeholder="供應商"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <date-range-picker v-model="modelCreatedAt" class="date-item" :fix-date.sync="query.createTime" />
        <!-- 搜索 -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="left">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="handleEdit"
          >
            新增采購單
          </el-button>
        </template>
      </crudOperation>
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column
          type="selection"
          :row-key="getRowKeys"
        />
        <el-table-column
          prop="code"
          label="采購單編號"
        />
        <el-table-column
          prop="vendorId"
          label="供應商"
        />
        <el-table-column
          prop="description"
          label="采購單描述"
        />
        <el-table-column
          prop="username"
          label="操作員工"
        />
        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.row)"
            >修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPurchase from '@/api/erp/purchase'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { name: null }
export default {
  name: 'Purchase',
  components: { pagination, rrOperation, crudOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['inBillType'],
  cruds() {
    return CRUD({
      title: '采購單', url: 'purchase/page', query: { 'deleteStatus': false }, sort: 'id:desc',
      crudMethod: { ...crudPurchase }, optShow: {
        add: false,
        edit: false,
        del: true,
        download: false,
        reset: false
      }
    })
  },
  data() {
    return {
      modelCreatedAt: '', // 时间选择器显示时间
      paymentStatus: '',
      options: [{
        value: 'true',
        label: '已支付'
      }, {
        value: 'false',
        label: '未支付'
      }],
      ids: [],
      permission: {
        add: ['admin', 'product:add'],
        edit: ['admin', 'product:edit'],
        del: ['admin', 'product:del']
      },
      selectedData: [],
      productId: [],
      queryTypeOptions: [
        { key: 'nameCn', display_name: '中文商品名称' }
      ],
      dialogVisible: false,
      tableData: []
    }
  },
  methods: {
    getModiFyKey() {
      if (this.ids.length > 1) {
        this.$message.error('不能同时修改多条数据')
        return false
      }
      if (this.ids.length === 0) {
        this.$message.error('请选择修改的采购单')
        return false
      }
      return true
    },
    getRowKeys(row) {
      this.ids.push(row.id)
      return row.id
    },
    handleEdit(row) {
      const purchaseId = row.id
      this.$router.push({ path: '/erp/creatPurchase/' + (purchaseId || 'add') })
    }
  }
}
</script>

<style scoped>
.crud-opts {
  padding: 4px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}

.crud-opts .crud-opts-right {
  margin-left: auto;
}

/*.buttonDiv{*/
/*  top: 80%;*/
/*  position: absolute;*/
/*  right: 20%;*/
/*}*/
</style>
